package com.ysd.twocrm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ysd.twocrm.entity.Leavetable;
import com.ysd.twocrm.mapper.LeavetableMapper;
import com.ysd.twocrm.service.LeaveTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/leavetable")
public class LeavetableController {
    @Autowired
    private LeavetableMapper leavetableMapper;
    @Autowired
    private LeaveTableService leaveTableService;

    /*
     * @Description  判断今天是否请假，如果请假返回他的状态
     * @param lt
     * @return java.lang.Object
     * @author liusupeng
     * @date 2020/5/22 15:07
     */
    @GetMapping
    public Object getAllIfLeave(Leavetable lt){
           SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String begintime = sdf.format(lt.getBegintime());
        QueryWrapper<Leavetable> queryWrapper=new QueryWrapper<>();
        queryWrapper.like("begintime",begintime).eq("lname",lt.getLname()).eq("userId",lt.getUserId());
        Leavetable leavetable = leavetableMapper.selectOne(queryWrapper);
        if(leavetable==null){
            return "false";
        }else{
            return leavetable;
        }

    }
    /*
     * @Description 查看全部未审批的数量
     * @param
     * @return java.lang.Object
     * @author liusupeng
     * @date 2020/5/22 15:40
     */
@GetMapping("/1")
    public Object getAllSunLeave(){

        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("lstatus",1);
    return leavetableMapper.selectCount(queryWrapper);
    }
    /*
     * @Description layui样式，查询全部写了请假条但是没有批准的数据
     * @param page
     * @return java.lang.Object
     * @author liusupeng
     * @date 2020/5/22 16:13
     */
    @GetMapping("/2")
    public Object findAllStatusOne(Integer page,Integer Lstatus){


    return leaveTableService.LEAVETABLE_LAYUI_UTILS(page,Lstatus);
    }

    /*
     * @Description   向请假表里添加数据
     * @param lt
     * @return java.lang.Object
     * @author liusupeng
     * @date 2020/5/22 15:05
     */
    @PostMapping
    public Object getAllAddLeavetable(Leavetable lt) {

        lt.setLsubmit(new Date());
        int insert = leavetableMapper.insert(lt);
        if(insert>0){
            return "success";
        }
        return "false";
    }
    /*
     * @Description 根据主键来修改，需要修改状态，以及加上审批人
     * @param l
     * @return java.lang.Object
     * @author liusupeng
     * @date 2020/5/22 16:26
     */
@PutMapping
    public Object exitLeaveStatus(Leavetable l){


        int i = leavetableMapper.updateById(l);
        if(i>0){
            return "success";
        }else{
            return "false";
        }
    }
}
